package com.system.freemarker;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.context.annotation.Configuration;

import com.jagregory.shiro.freemarker.ShiroTags;

//1：引入依赖包
/*
<dependency>   
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-freeMarker</artifactId>
</dependency>
*/
//2、freemaker配置项
/*
# ======================================================
# FREEMARKER (FreeMarkerAutoConfiguration)
# ======================================================
spring.freemarker.allowRequestOverride=false
spring.freemarker.allowSessionOverride=false
spring.freemarker.cache=true
spring.freemarker.checkTemplateLocation=true
spring.freemarker.contentType=text/html
spring.freemarker.exposeRequestAttributes=false
spring.freemarker.exposeSessionAttributes=true
spring.freemarker.exposeSpringMacroHelpers=true
spring.freemarker.allowSessionOverride=true
#spring.freemarker.prefix=
spring.freemarker.requestContextAttribute=requestContext
#spring.freemarker.settings.*=
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.suffix=.html
spring.freemarker.templateEncoding=UTF-8
spring.freemarker.templateLoaderPath=classpath:/templates/
# whitelist of view names that can be resolved
#spring.freemarker.viewNames=*.ftl
# view class
#spring.freemarker.viewClass=com.igap.data.utils.DzzwMarkerView
# =====================================================
//3、编写ftl文件，路径src/main/resources/templates
 */
/**
 * Freemarker tags 
 * @author jpx
 */
@Configuration
//@ConfigurationProperties(value = "spring.freemarker", ignoreNestedProperties = false)
public class FreeMarkerConfig extends FreeMarkerAutoConfiguration{
	/**
	 * Date Format
	 */
	private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
	/**
	 * Date Time Format
	 */
	private static final String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	/*
	 * 数字不带逗号
	 */
	private static final String NUMBER_FORMAT_PATTERN = "#";
	/**
	 * freemarker configuration
	 */
    @Autowired
    private freemarker.template.Configuration configuration;
    
    /**
     * 自动导入Map
     */
    private Map<String,String> autoImportMap = new HashMap<String,String>();
    /**
     * 设置共享变量,仅创建时执行
     */
    @PostConstruct
    public void  setSharedVariable(){
        configuration.setDateFormat(DATE_FORMAT_PATTERN);
        configuration.setDateTimeFormat(DATETIME_FORMAT_PATTERN);
        configuration.setNumberFormat(NUMBER_FORMAT_PATTERN);
        /*同freemarker.properties里设置
        auto_import=/WEB-INF/template/common/common.ftl as webfinal
                      前台文件使用 其中m为ftl中的宏定义
            <@webfinal.m 'app.name'/>
         */
        //注入资源宏文件
        autoImportMap.put("webfinal", "common/ftl/webfinal.ftl");
        configuration.setAutoImports(autoImportMap);
        //页面支持shiro标签，需shiro-freemarker-tags
        configuration.setSharedVariable("shiro", new ShiroTags());
    }
}